<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['base:bcm:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:bcm:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:bcm:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['base:bcm:export']"
        >
          导出
        </el-button>
      </el-col>

      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="bcmList"
      height="calc(100% - 100px)"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="班次代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmBcdm"
      />
      <el-table-column
        label="班次名称"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmBcmc"
      />
      <el-table-column
        label="开始时间"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmKssj"
      />
      <el-table-column
        label="结束时间"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmJssj"
      />
      <el-table-column
        label="有效标志"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmFlag"
      >
        <template #default="scope">
          <dict-tag :options="bcmFlagOptions" :value="scope.row.bcmFlag" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmDesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bcmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.bcmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bcmGgry"
      />
      <el-table-column
        label="更改日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bcmGgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.bcmGgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:bcm:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:bcm:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" width="650px">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="班次名称" prop="bcmBcmc">
            <el-input
              v-model="queryParams.bcmBcmc"
              placeholder="请输入班次名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改班次代码对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班次代码" prop="bcmBcdm">
          <el-input v-model="form.bcmBcdm" placeholder="请输入班次代码" />
        </el-form-item>
        <el-form-item label="班次名称" prop="bcmBcmc">
          <el-input v-model="form.bcmBcmc" placeholder="请输入班次名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="bcmKssj">
          <el-input v-model="form.bcmKssj" placeholder="请输入开始时间" />
        </el-form-item>
        <el-form-item label="结束时间" prop="bcmJssj">
          <el-input v-model="form.bcmJssj" placeholder="请输入结束时间" />
        </el-form-item>
        <el-form-item label="有效标志">
          <el-radio-group v-model="form.bcmFlag">
            <el-radio
              v-for="dict in bcmFlagOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//const { proxy } = getCurrentInstance();

import {
  listBcm,
  getBcm,
  delBcm,
  addBcm,
  updateBcm,
  exportBcm,
} from "@/api/base/bcm";

export default {
  compatConfig: {
    MODE: 2, // 只为这个组件选择性启用 Vue 3 行为
  },
  name: "Bcm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      dialogVisible: false,
      // 总条数
      total: 0,
      // 班次代码表格数据
      bcmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 有效标志字典
      bcmFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        bcmBcmc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    const { sys_flag } = this.useDict("sys_flag");
    this.bcmFlagOptions = sys_flag;
  },
  methods: {
    /** 查询班次代码列表 */
    getList() {
      this.loading = true;
      this.queryParams.pageNum = 1;
      listBcm(this.queryParams).then((response) => {
        this.bcmList = [];
        this.bcmList = this.bcmList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listBcm(this.queryParams).then((response) => {
          this.bcmList = this.bcmList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bcmBcdm: null,
        bcmBcmc: null,
        bcmKssj: null,
        bcmJssj: null,
        bcmFlag: 0,
        bcmDesc: null,
        bcmJlry: null,
        bcmJlrq: null,
        bcmGgry: null,
        bcmGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.bcmList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bcmBcdm);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班次代码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bcmBcdm = row.bcmBcdm || this.ids;
      getBcm(bcmBcdm).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班次代码";
      });
    },
    handleSearch() {
      //this.reset();
      this.title = "查询班次代码";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改班次代码") {
            updateBcm(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBcm(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bcmBcdms = row.bcmBcdm || this.ids;
      this.$confirm(
        '是否确认删除班次代码编号为"' + bcmBcdms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delBcm(bcmBcdms);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有班次代码数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportBcm(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
